﻿using UnityEngine;
using System.Collections;

public class RotateParticle : MonoBehaviour
{
    [SerializeField]
    private float delay = 0f;
    [SerializeField]
    bool loop = false;
    [SerializeField]
    float speed = 1f;//one loop time
    [SerializeField]
    bool direction = true;
    [SerializeField]
    ERoateAround around;

    public float Speed
    {
        get { return speed; }
        set { speed = value; }
    }

    float rotateBeginTime;
    Quaternion originAngle;
    Quaternion targetAngle;
    float rotated = 0f;

    enum ERoateAround
    {
        Forward,
        Right,
        Up
    }
    // Use this for initialization
    void Start()
    {
        rotateBeginTime = Time.time + delay;
        originAngle = transform.rotation;
    }

    // Update is called once per frame
    void Update()
    {
        if (rotateBeginTime < Time.time)
        {
            float angle = speed * (direction ? 1 : -1);
            float angleDelta = angle * Time.deltaTime;
            transform.Rotate(GetAround(), angleDelta, Space.Self);
            rotated += Mathf.Abs(angleDelta);
            if (rotated >= 360f && loop == false)
            {
                enabled = false;
            }
        }
    }

    private Vector3 GetAround()
    {
        if (around == ERoateAround.Forward)
        {
            //return transform.forward;
            return Vector3.forward;
        }
        else if (around == ERoateAround.Right)
        {
            //return transform.right;
            return Vector3.right;
        }
        else
        {
            //return transform.up;
            return Vector3.up;
        }
    }
}
